import seal from '@/lib/request/seal'
import { RootState } from '../index'
import { Commit } from 'vuex'
import { delay } from '@/lib/utils'

import { IResponse } from '@/lib/service'
import organize from '@/lib/request/organize'
export interface ApplyPageState {
  type: string
  printVisible: false
  orgList: any[]
  auditOrgList: any[]
  visible: boolean

  applyLicenseNumberData?: any
}
const initState: ApplyPageState = {
  type: '',
  printVisible: false,
  orgList: [],
  auditOrgList: [],
  visible: false,
  applyLicenseNumberData: {},
}

const getters = {}

const actions = {
  queryOrg(
    context: {
      commit: Commit
      state: ApplyPageState
      rootState: RootState
      getters: any
    },
    payload: any
  ) {
    if (!payload) return
    delay(async () => {
      const data: IResponse = await seal.queryOrg(payload)
      if (data && data.data) {
        context.commit('setStateData', {
          orgList: (data.data.list || []).map((item: any, index: number) => {
            return {
              organizeName: item.fullName,
              organizeUUID: item.uuid,
              organizeAdministrationLevel: item.administrationLevel,
              organizeLicenseNumber: item.licenseNumber || item.organizeCode,
              organizeLicenseType: item.licenseType,
              organizeType: item.organizeType,
            }
          }),
        })
      }
    }, 200)
  },
  async queryAuditOrg(
    context: {
      commit: Commit
      state: ApplyPageState
      rootState: RootState
      getters: any
    },
    payload: any
  ) {
    if (!payload) return
    const data: IResponse = await seal.queryAuditOrg(payload)
    if (data && data.data) {
      context.commit('setStateData', {
        auditOrgList: (data.data.list || []).map((item: any, index: number) => {
          return {
            organizeName: item.fullName,
            organizeUUID: item.uuid,
            organizeAdministrationLevel: item.administrationLevel,
            organizeLicenseNumber: item.licenseNumber || item.organizeCode,
            organizeLicenseType: item.licenseType,
            organizeType: item.organizeType,
          }
        }),
      })
      // return data
    }
  },
  getAreas(
    context: {
      commit: Commit
      state: ApplyPageState
      rootState: RootState
      getters: any
    },
    payload: any
  ) {
    return organize.getArea().then((data: IResponse) => data.data)
  },
  getInitTree(
    context: {
      commit: Commit
      state: ApplyPageState
      rootState: RootState
      getters: any
    },
    payload: any
  ) {
    return organize
      .getTreeWithAreaAndName(payload.uuid, payload.fullName)
      .then((data: IResponse) => data.data)
  },
  createOrder(
    context: {
      commit: Commit
      state: ApplyPageState
      rootState: RootState
      getters: any
    },
    payload: any
  ) {
    // 创建订单
    return seal.createOrder(payload).then((data: IResponse) => data.data)
  },
  querySealApplyPayOrderInfo(
    context: {
      commit: Commit
      state: ApplyPageState
      rootState: RootState
      getters: any
    },
    payload: any
  ) {
    // 查询订单
    return seal
      .querySealApplyPayOrderInfo(payload)
      .then((data: IResponse) => data.data)
  },
  finishQRCodePayOrder(
    context: {
      commit: Commit
      state: ApplyPageState
      rootState: RootState
      getters: any
    },
    payload: any
  ) {
    // 查询订单
    return seal
      .finishQRCodePayOrder(payload.sealApplyId, payload.orderNumber)
      .then((data: IResponse) => data.data)
  },
  sealApplyUnifiedSubmit(
    context: {
      commit: Commit
      state: ApplyPageState
      rootState: RootState
      getters: any
    },
    payload: any
  ) {
    // 提交订单
    return seal
      .sealApplyUnifiedSubmit(payload)
      .then((data: IResponse) => data.data)
  },
  cancelApplyManager(
    // 管理员解绑
    context: {
      commit: Commit
      state: ApplyPageState
      rootState: RootState
      getters: any
    },
    payload: any
  ) {
    return seal.cancelApplyManager(payload)
  },
  queryChildAndSealCount(
    // 查询管理员数据
    context: {
      commit: Commit
      state: ApplyPageState
      rootState: RootState
      getters: any
    },
    payload: any
  ) {
    return seal.queryChildAndSealCount(payload)
  },
  createElectronicSealTask(
    // 创建名章绘制任务
    context: {
      commit: Commit
      state: ApplyPageState
      rootState: RootState
      getters: any
    },
    payload: any
  ) {
    return seal.createElectronicSealTask(payload)
  },
  queryElectronicSealPic(
    // 获取名章绘制图片
    context: {
      commit: Commit
      state: ApplyPageState
      rootState: RootState
      getters: any
    },
    payload: any
  ) {
    return seal.queryElectronicSealPic(payload)
  },
  submitSealElectronicPic(
    // 名章绘制图片上传
    context: {
      commit: Commit
      state: ApplyPageState
      rootState: RootState
      getters: any
    },
    payload: any
  ) {
    return seal.submitSealElectronicPic(payload)
  },
}

const mutations = {
  setStateData(state: ApplyPageState, data: any) {
    // eslint-disable-next-line no-param-reassign
    state = Object.assign(state, data)
  },
}

export default {
  namespaced: true,
  state: initState,
  getters,
  actions,
  mutations,
}
